package com.cskaoyan.service;

import com.cskaoyan.bean.MarketKeyword;
import com.cskaoyan.bean.MarketKeywordExample;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.bean.vo.KeywordVo;
import com.cskaoyan.mapper.MarketKeywordMapper;
import com.cskaoyan.util.MyBatisUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.session.SqlSession;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class KeywordServiceImpl implements KeywordService {
    @Override
    public BaseRespVo list(Integer page, Integer limit, String keyword, String url, String sort, String order) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        MarketKeywordMapper keywordMapper = sqlSession.getMapper(MarketKeywordMapper.class);

        PageHelper.startPage(page, limit);

        MarketKeywordExample marketKeywordExample = new MarketKeywordExample();
        MarketKeywordExample.Criteria marketKeywordExampleCriteria = marketKeywordExample.createCriteria();
        if (keyword != null && keyword != "") {
            marketKeywordExampleCriteria.andKeywordLike("%"+keyword+"%");
        }
        if (url != null && url != "") {
            marketKeywordExampleCriteria.andUrlLike("%"+url+"%");
        }
        marketKeywordExampleCriteria.andDeletedEqualTo(false);

        List<MarketKeyword> marketKeywords = keywordMapper.selectByExample(marketKeywordExample);

        PageInfo pageInfo = new PageInfo(marketKeywords);
        sqlSession.close();// 不要忘记
        return BaseRespVo.ok(CommonData.data(pageInfo));
    }

    @Override
    public BaseRespVo create(MarketKeyword marketKeyword) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        MarketKeywordMapper keywordMapper = sqlSession.getMapper(MarketKeywordMapper.class);

        Date date = new Date();
        if (marketKeyword.getUrl()==null){
            marketKeyword.setUrl("");
        }
        // if (marketKeyword.getKeyword()==null){
        //     marketKeyword.setKeyword("");
        // }
        marketKeyword.setAddTime(date);
        marketKeyword.setUpdateTime(date);
        marketKeyword.setDeleted(false);

        //useGeneratedKeys="true" keyProperty="id"
        keywordMapper.insertSelective(marketKeyword);

        MarketKeywordExample marketKeywordExample = new MarketKeywordExample();
        MarketKeywordExample.Criteria marketKeywordExampleCriteria = marketKeywordExample.createCriteria();
        marketKeywordExampleCriteria.andKeywordEqualTo(marketKeyword.getKeyword());
        marketKeywordExampleCriteria.andUrlEqualTo(marketKeyword.getUrl());

        MarketKeyword keyword = keywordMapper.selectByExample(marketKeywordExample).get(0);

        sqlSession.commit();
        sqlSession.close();// 不要忘记
        return BaseRespVo.ok(keyword);
    }

    @Override
    public BaseRespVo update(KeywordVo keywordVo) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        MarketKeywordMapper keywordMapper = sqlSession.getMapper(MarketKeywordMapper.class);

        Date date = new Date();

        MarketKeyword marketKeyword = keywordMapper.selectByPrimaryKey(keywordVo.getId());
        marketKeyword.setKeyword(keywordVo.getKeyword());
        marketKeyword.setIsDefault(keywordVo.getIsDefault());
        marketKeyword.setIsHot(keywordVo.getIsHot());
        marketKeyword.setUrl(keywordVo.getUrl());
        marketKeyword.setUpdateTime(date);

        keywordMapper.updateByPrimaryKey(marketKeyword);

        sqlSession.commit();
        sqlSession.close();// 不要忘记
        return BaseRespVo.ok(marketKeyword);
    }

    @Override
    public BaseRespVo delete(KeywordVo keywordVo) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        MarketKeywordMapper keywordMapper = sqlSession.getMapper(MarketKeywordMapper.class);
        Integer id = keywordVo.getId();

        MarketKeyword marketKeyword = keywordMapper.selectByPrimaryKey(id);
        marketKeyword.setDeleted(true);
        keywordMapper.updateByPrimaryKey(marketKeyword);

        sqlSession.commit();
        sqlSession.close();// 不要忘记
        return BaseRespVo.ok();
    }
}
